/**
 * Copyright (c) [2025] [AGIROS] 
 * [travoddsgen] is licensed under Mulan PSL v2. 
 * You can use this software according to the terms and conditions of the Mulan PSL v2. 
 * You may obtain a copy of Mulan PSL v2 at: 
 *          http://license.coscl.org.cn/MulanPSL2 
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 
 * See the Mulan PSL v2 for more details.
 */ 

#include "CppLangTypeSupportHeaderVisitor.h"
#include <fstream>

CppLangTypeSupportHeaderVisitor::CppLangTypeSupportHeaderVisitor(
    const std::string& outputDir, 
    const std::string& fileName, 
    CppLangTypeNameVisitor* typeNameVisitor, 
    const std::string& headerFile)
    : FileGenVisitor(outputDir, fileName),
      typeNameVisitor_(typeNameVisitor),
      headerFile_(headerFile) {
}

void CppLangTypeSupportHeaderVisitor::visit(SpecificationNode& node) {
    // 从文件名中提取不带后缀的文件名
    std::string fileName = fileName_.substr(0, fileName_.find_last_of("."));
    if (!fileName_.empty()) {
        // 文件头的宏声明
        std::string headerGuard = "#ifndef " + fileName + "_H\n";
        headerGuard += "#define " + fileName + "_H\n";
        content_ += headerGuard + "\n";
    }
    // 头文件包含
    std::string includes = "";
    includes += "#include \"dcps/topic/typesupport.h\"\n";
    std::string headerFile = headerFile_;
    // 文件头的宏声明
    std::string headers = "#include \"" + headerFile + "\"\n";
    includes += headers + "\n";
    content_ += includes;

    // __cplusplus宏定义开始
    content_ += "#ifdef __cplusplus\n";
    content_ += "extern \"C\" {\n";
    content_ += "#endif\n\n";

    // 递归访问子节点
    if (node.definitions_) {
        node.definitions_->accept(*this);
    }

    // __cplusplus宏定义结尾
    content_ += "#ifdef __cplusplus\n";
    content_ += "}\n";
    content_ += "#endif\n";

    if (!fileName_.empty()) {
        // 文件尾的宏声明
        std::string endGuard = "#endif /* " + fileName + "_H */\n";
        content_ += endGuard;
    }
    // 将_content写入文件
    writeToFile();
}

void CppLangTypeSupportHeaderVisitor::visit(ModuleDclNode& node) {
    // namespace开始
    content_ += "namespace " + *node.identifier_ + " {\n\n";
    // 递归访问子节点
    for (auto& def : node.definitions_->nodes_) {
        def->accept(*this);
    }
    // namespace结束
    content_ += "} // namespace " + *node.identifier_ + "\n\n";
}

void CppLangTypeSupportHeaderVisitor::visit(StructDefNode& node) {
    // 声明模块前缀_结构体名的结构体
    std::string structName = typeNameVisitor_->findShortName(&node);
    std::string structDefHeader = "class DDS_TYPE_API " + structName + "TypeSupport : public AGIDDS::TypeSupport {\n";
    structDefHeader += "public:\n";
    // 结构体定义开始
    content_ += structDefHeader;
    indentLevel_++;
    // 单例模式的get_instance函数
    content_ += indent() + "static " + structName + "TypeSupport* get_instance();\n\n";
    // 实现基类的方法
    // create_data
    content_ += indent() + "virtual void* create_data() override;\n\n";
    // delete_data
    content_ += indent() + "virtual void delete_data(void* data) override;\n\n";
    // copy_data
    content_ += indent() + "virtual int copy_data(void* dst, void* src) override;\n\n";
    // get_serialized_data_size
    content_ += indent() + "virtual unsigned int get_serialized_data_size(void* data, unsigned int currentAlignment) override;\n\n";
    // get_max_serialized_data_size
    content_ += indent() + "virtual unsigned int get_max_serialized_data_size(void* data, unsigned int currentAlignment) override;\n\n";
    // serialize_data
    content_ += indent() + "virtual int serialize_data(void* data, AGIDDSCdrSerializer* cdr, int endian) override;\n\n";
    // deserialize_data
    content_ += indent() + "virtual int deserialize_data(void* data, AGIDDSCdrDeserializer* cdr, int endian) override;\n\n";
    // 获取数据类型的类型对象的函数
    content_ += indent() + "virtual AGIDDS::TypeObject* get_typeobject() override;\n\n";
    // get_instancehandle
    content_ += indent() + "virtual AGIDDS::ReturnCode_t get_instancehandle(void* data, AGIDDSCdrSerializer* cdr, AGIDDS::InstanceHandle_t& iHandle, bool forceMd5 = false) override;\n\n";
    // has_key
    content_ += indent() + "virtual bool has_key() override;\n\n";
    // get_typename
    content_ += indent() + "virtual const char* get_typename() override;\n\n";
    // MakeKey
    content_ += indent() + "virtual int MakeKey(const void* data, AGIDDS::InstanceHandle_t& iHandle, bool forceMd5 = false) override;\n\n";
    // serialize_key
    content_ += indent() + "int serialize_key(void* data, AGIDDSCdrSerializer* cdr, int endian);\n\n";
    content_ += "private:\n";
    // 禁用构造函数
    content_ += indent() + structName + "TypeSupport() = default;\n";
    indentLevel_--;
    // 结构体定义结束
    content_ += "};\n\n";
}

void CppLangTypeSupportHeaderVisitor::visit(UnionDefNode& node) {
    // 声明模块前缀_结构体名的结构体
    std::string structName = typeNameVisitor_->findShortName(&node);
    std::string structDefHeader = "class DDS_TYPE_API " + structName + "TypeSupport : public AGIDDS::TypeSupport {\n";
    structDefHeader += "public:\n";
    // 结构体定义开始
    content_ += structDefHeader;
    indentLevel_++;
    // 单例模式的get_instance函数
    content_ += indent() + "static " + structName + "TypeSupport* get_instance();\n\n";
    // 实现基类的方法
    // create_data
    content_ += indent() + "virtual void* create_data() override;\n\n";
    // delete_data
    content_ += indent() + "virtual void delete_data(void* data) override;\n\n";
    // copy_data
    content_ += indent() + "virtual int copy_data(void* dst, void* src) override;\n\n";
    // get_serialized_data_size
    content_ += indent() + "virtual unsigned int get_serialized_data_size(void* data, unsigned int currentAlignment) override;\n\n";
    // get_max_serialized_data_size
    content_ += indent() + "virtual unsigned int get_max_serialized_data_size(void* data, unsigned int currentAlignment) override;\n\n";
    // serialize_data
    content_ += indent() + "virtual int serialize_data(void* data, AGIDDSCdrSerializer* cdr, int endian) override;\n\n";
    // deserialize_data
    content_ += indent() + "virtual int deserialize_data(void* data, AGIDDSCdrDeserializer* cdr, int endian) override;\n\n";
    // 获取数据类型的类型对象的函数
    content_ += indent() + "virtual AGIDDS::TypeObject* get_typeobject() override;\n\n";
    // get_instancehandle
    content_ += indent() + "virtual AGIDDS::ReturnCode_t get_instancehandle(void* data, AGIDDSCdrSerializer* cdr, AGIDDS::InstanceHandle_t& iHandle, bool forceMd5 = false) override;\n\n";
    // has_key
    content_ += indent() + "virtual bool has_key() override;\n\n";
    // get_typename
    content_ += indent() + "virtual const char* get_typename() override;\n\n";
    // MakeKey
    content_ += indent() + "virtual int MakeKey(const void* data, AGIDDS::InstanceHandle_t& iHandle, bool forceMd5 = false) override;\n\n";
    // serialize_key
    content_ += indent() + "int serialize_key(void* data, AGIDDSCdrSerializer* cdr, int endian);\n\n";
    content_ += "private:\n";
    // 禁用构造函数
    content_ += indent() + structName + "TypeSupport() = default;\n";
    indentLevel_--;
    // 结构体定义结束
    content_ += "};\n\n";
}